﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ArbolandiaBL;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var offers = new ServiceUI().GetOfertasDestacadosHome().Where(x => x.IsOferta == true).OrderBy(x => Guid.NewGuid()).Take(4).ToList();
            for (int i = 0; i < offers.Count(); i++)
            {
                //Main container
                var item = new HtmlGenericControl("div");
                if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31)
                {
                    item.Attributes.Add("class", "list-item-right");
                }
                else
                {
                    item.Attributes.Add("class", "list-item");
                }

                //Control for link
                var link = new HyperLink();
                link.NavigateUrl = "~/Detail.aspx?d=" + offers[i].Id;

                //Main image
                var img = new Image();
                img.ImageUrl = offers[i].ImageUrl;
                img.CssClass = "list-item-image";

                //Product name and lower price
                var info = new HtmlGenericControl("div");
                info.Attributes.Add("class", "list-item-info");

                var infotext = new HtmlGenericControl("div");
                infotext.Attributes.Add("class", "list-item-info-text");

                var infotextproductname = new Label();
                infotextproductname.CssClass = "list-item-info-text-product";
                infotextproductname.Text = offers[i].Name;

                var infotextlowerprice = new Label();
                infotextlowerprice.CssClass = "list-item-info-text-lower";
                infotextlowerprice.Text = "Desde " + offers[i].LowerPrice.ToString("C") + " pesos";

                var cartimage = new Image();
                cartimage.Attributes.Add("class", "list-item-text-cart");
                cartimage.ImageUrl = "~/Images/carrito.png";

                infotext.Controls.Add(infotextproductname);
                infotext.Controls.Add(infotextlowerprice);
                info.Controls.Add(infotext);
                info.Controls.Add(cartimage);
                info.Controls.Add(new HtmlGenericControl("br"));
                link.Controls.Add(img);
                link.Controls.Add(info);
                item.Controls.Add(link);
                homeOffers.Controls.Add(item);
            }

            homeOffers.Controls.Add(new HtmlGenericControl("br"));

            var hots = new ServiceUI().GetOfertasDestacadosHome().Where(x => x.IsDestacado == true).OrderBy(x => Guid.NewGuid()).Take(8).ToList();
            for (int i = 0; i < hots.Count(); i++)
            {
                //Main container
                var item = new HtmlGenericControl("div");
                if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31)
                {
                    item.Attributes.Add("class", "list-item-right");
                }
                else
                {
                    item.Attributes.Add("class", "list-item");
                }

                //Control for link
                var link = new HyperLink();
                link.NavigateUrl = "~/Detail.aspx?d=" + hots[i].Id;

                //Main image
                var img = new Image();
                img.ImageUrl = hots[i].ImageUrl;
                img.CssClass = "list-item-image";

                //Product name and lower price
                var info = new HtmlGenericControl("div");
                info.Attributes.Add("class", "list-item-info");

                var infotext = new HtmlGenericControl("div");
                infotext.Attributes.Add("class", "list-item-info-text");

                var infotextproductname = new Label();
                infotextproductname.CssClass = "list-item-info-text-product";
                infotextproductname.Text = hots[i].Name;

                var infotextlowerprice = new Label();
                infotextlowerprice.CssClass = "list-item-info-text-lower";
                infotextlowerprice.Text = "Desde " + hots[i].LowerPrice.ToString("C") + " pesos";

                var cartimage = new Image();
                cartimage.Attributes.Add("class", "list-item-text-cart");
                cartimage.ImageUrl = "~/Images/carrito.png";

                infotext.Controls.Add(infotextproductname);
                infotext.Controls.Add(infotextlowerprice);
                info.Controls.Add(infotext);
                info.Controls.Add(cartimage);
                info.Controls.Add(new HtmlGenericControl("br"));
                link.Controls.Add(img);
                link.Controls.Add(info);
                item.Controls.Add(link);
                homeHots.Controls.Add(item);
            }

            homeHots.Controls.Add(new HtmlGenericControl("br"));
            LoadBanners();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void LoadBanners()
    {
        try
        {

            var banner1 = new ServiceUI().GetBanner(BannerType.Top);
            for (int i = 0; i < banner1.Count(); i++)
            {
                var c = new HyperLink() { ImageUrl = banner1[i].Url, NavigateUrl = banner1[i].LinkUrl };
                var p = new HyperLink() { NavigateUrl = banner1[i].LinkUrl, Text = ((i + 1)).ToString() };
                //c.Target = "_blank";
                p.Attributes.Add("rel", ((i + 1)).ToString());
                imagereel.Controls.Add(c);
                paging.Controls.Add(p);
            }
            var banner2 = new ServiceUI().GetBanner(BannerType.Middle);
            for (int i = 0; i < banner2.Count(); i++)
            {
                var c = new HyperLink() { ImageUrl = banner2[i].Url, NavigateUrl = banner2[i].LinkUrl };
                var p = new HyperLink() { NavigateUrl = banner2[i].LinkUrl, Text = ((i + 1)).ToString() };
                //c.Target = "_blank";
                p.Attributes.Add("rel", ((i + 1)).ToString());
                imagereel2.Controls.Add(c);
                paging2.Controls.Add(p);
            }
            var banner3 = new ServiceUI().GetBanner(BannerType.Bottom);
            for (int i = 0; i < banner3.Count(); i++)
            {
                var c = new HyperLink() { ImageUrl = banner3[i].Url, NavigateUrl = banner3[i].LinkUrl };
                var p = new HyperLink() { NavigateUrl = banner3[i].LinkUrl, Text = ((i + 1)).ToString() };
                //c.Target = "_blank";
                p.Attributes.Add("rel", ((i + 1)).ToString());
                imagereel3.Controls.Add(c);
                paging3.Controls.Add(p);
            }
            var banner4 = new ServiceUI().GetBanner(BannerType.Home);
            for (int i = 0; i < banner4.Count(); i++)
            {
                var c = new HyperLink() { ImageUrl = banner4[i].Url, NavigateUrl = banner4[i].LinkUrl };
                var p = new HyperLink() { NavigateUrl = banner4[i].LinkUrl, Text = ((i + 1)).ToString() };
                //c.Target = "_blank";
                p.Attributes.Add("rel", ((i + 1)).ToString());
                imagereel4.Controls.Add(c);
                paging4.Controls.Add(p);
            }
        }
        catch (Exception)
        {

            throw;
        }

    }

}